<?php

namespace App\Admin\Controllers;

use App\Models\Vendor;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class VendorsController extends AdminController
{
    protected $title = '小卖部供应商';
    
    protected function grid()
    {
        $grid = new Grid(new Vendor());

        $grid->column('id', 'ID');
        $grid->column('name', '名称');
        $grid->column('phone', '手机');
        $grid->column('created_at', '创建时间')->display(function () {
        	return $this->created_at->toDatetimeString();
        });
        
        $grid->disableRowSelector();
        $grid->disableExport();

        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(Vendor::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('name', '供应商名称');
        $show->field('phone', '供应商手机');
        $show->field('initial_code', '初始密码');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        return $show;
    }
	
	protected function form()
    {
        $form = new Form(new Vendor());

        $form->text('name', '供应商名称')
	        ->creationRules('required|unique:vendors')
	        ->updateRules('required|unique:vendors,name,{{id}}');
        
        $form->mobile('phone', '供应商手机')
	        ->creationRules('required|unique:vendors')
	        ->updateRules('required|unique:vendors,phone,{{id}}');
        
        $form->text('initial_code', '初始密码')
	        ->placeholder('初始密码')
	        ->readonly();
        
	    $form->radio('is_disabled', '取消合作')
		    ->options(config('site.bool'))
		    ->default(false);
	    
        return $form;
    }
}
